package com.flyqiu.flow.model.entity.hi;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.flyqiu.common.tools.LocalDateTimeTools;
import com.flyqiu.flow.common.std.InstDateTimeComplete;
import com.flyqiu.flow.model.entity.ru.ProcRuInst;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * 流程历史信息
 */
@EqualsAndHashCode(callSuper = true)
@Data
@TableName(ProcHiInst.TABLE_NAME)
public class ProcHiInst extends ProcRuInst implements InstDateTimeComplete {
    public static final String TABLE_NAME = "FLY_FLOW_HI_INST";

    /**
     * 流程结束时间
     */
    @TableField("END_TIME_")
    private LocalDateTime endTime;
    /**
     * 流程耗时（毫秒）
     */
    @TableField("DURATION_")
    private Long duration;

    public static ProcHiInst create(ProcRuInst procInst) {
        ProcHiInst procHiInst = new ProcHiInst();
        procHiInst.setId(procInst.getId());
        procHiInst.setProcInstName(procInst.getProcInstName());
        procHiInst.setProcDeployDefId(procInst.getProcDeployDefId());
        procHiInst.setProcDeployDefKey(procInst.getProcDeployDefKey());
        procHiInst.setParentId(procInst.getParentId());
        procHiInst.setParentProcDefKey(procInst.getParentProcDefKey());
        procHiInst.setParentLevelDefKeys(procInst.getParentLevelDefKeys());
        procHiInst.setBusinessKey(procInst.getBusinessKey());
        procHiInst.setBusinessId(procInst.getBusinessId());
        procHiInst.setBusinessParams(procInst.getBusinessParams());
        procHiInst.setStartUserKey(procInst.getStartUserKey());
        procHiInst.setStartUserId(procInst.getStartUserId());
        procHiInst.setStartUserAttr(procInst.getStartUserAttr());
        procHiInst.setStatus(procInst.getStatus());
        procHiInst.setCreateTime(procInst.getCreateTime());
        procHiInst.setTenantId(procInst.getTenantId());
        return procHiInst;
    }

    @Override
    public void completeEndTime(LocalDateTime endTime) {
        setEndTime(endTime);
        duration = LocalDateTimeTools.timeDiffMillis(getCreateTime(), endTime);
    }
}
